import * as apis from '../services/backstageManagementService.js'

export default {
    namespace:'backstageManagement',

    state:{
        userList:[],
        administratorsList:[]
    },

    reducers:{
        setUserList:setUserList,
        setAdministratorList:setAdministratorList
    },

    effects:{
        getUserTableData:getUserTableData,
        changePasswordById:changePasswordById
    }
}

function setAdministratorList(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state.administratorsList = payload.data 
    return _state
}

function setUserList(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state.userList = payload.data 
    return _state
}

/**
 * 获取所有用户列表 
 */
function* getUserTableData(payload,{select,call,put}){
    let result = yield call(apis.getUserTableData)
    let administratorsList = []
    let userList = []
    for(let i=0;i<result.data.data.length;i++){
        for(let key in result.data.data[i]){
            if(key == "userType"){
                if(result.data.data[i][key] == 0){
                    result.data.data[i][key] = "超级管理员"
                    userList.push(result.data.data[i])
                }
                if(result.data.data[i][key] == 1){
                    result.data.data[i][key] = "技术供应商"
                    userList.push(result.data.data[i])
                }
                if(result.data.data[i][key] == 4){
                    result.data.data[i][key] = "机构"
                    userList.push(result.data.data[i])
                }
                if(result.data.data[i][key] == 3){
                    administratorsList.push(result.data.data[i])
                }
            }  
        }
    }
    yield put({
        type:"backstageManagement/setAdministratorList",
        data:administratorsList
    })

    yield put({
        type:"backstageManagement/setUserList",
        data:userList
    })
}

/**
 * 修改用户密码（重置密码）
 */
function* changePasswordById(payload,{select,call,put}){
    let result = yield call(apis.changePasswordById,payload.data)

    yield put({
        type:"app/setServerRes",
        data:result.data
    })
}

